package edu.ucsd.cse135.gas.action.admin;

import java.util.ArrayList;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.ucsd.cse135.gas.bean.admin.PopulateDatabaseBean;
import edu.ucsd.cse135.gas.logic.admin.PopulateDatabase;
import edu.ucsd.cse135.gas.logic.support.Support;
import edu.ucsd.cse135.gas.resources.Constants;

public class PopulateDatabaseAction extends Action {

	public PopulateDatabaseAction()
	{
		super();
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	{
			
		/** Prepare support stuff and vector paths **/
			
		Support s = new Support();
		
		String path1 = request.getServletContext().getRealPath("res/inc/countries.txt");
		String path2 = request.getServletContext().getRealPath("res/inc/universities.txt");
		String path3 = request.getServletContext().getRealPath("res/inc/majors.txt");
		String path4 = request.getServletContext().getRealPath("res/inc/specializations.txt");
		    
		Vector countries = s.getCountries(path1);
		ArrayList<String> countriesAl = new ArrayList<String>(); 
		ArrayList<String> statesAl = new ArrayList<String>();
		    
		Vector universities = s.getUniversities(path2);
		ArrayList<ArrayList<String>> universitiesAl = new ArrayList<ArrayList<String>>();
				    
		Vector discipline = s.getMajors(path3);
		ArrayList<String> disciplinesAl = new ArrayList<String>();
				    
		Vector specializations = s.getSpecializations(path4);
		ArrayList<String> specializationsAl = new ArrayList<String>();
				    
		/** Call clean method **/
				   
		if(PopulateDatabase.clean())
		{
			PopulateDatabaseBean pdb = new PopulateDatabaseBean();
			
			pdb.setClean("Clean process was successful.");
				 
		    /** Get all states **/
				    
		    for(int i = 0; i<universities.size(); i++)
		    {
		    	Vector tuple = (Vector)universities.get(i);
		    	statesAl.add(tuple.get(0).toString());
				    	
		    	if(tuple.get(0).toString().equals("Wyoming"))
		    	{
		    		break;
		    	}
			}
				    
			/** Get all countries **/
				    
			for(int i = 0; i<countries.size(); i++)
			{
				countriesAl.add(countries.get(i).toString());
			}    
				    
		    /** Populate location table **/
				    
		    pdb.setLocations(PopulateDatabase.location(countriesAl, statesAl));
		    
		    /** Get all specializations **/
				    
		    for(int i = 0; i<specializations.size(); i++)
		    {
		    	specializationsAl.add(specializations.get(i).toString());
		    }
				    
		    /** Populate specialization table **/
		    
			pdb.setSpecializations(PopulateDatabase.specialization(specializationsAl));
		    	
		    /** Get all majors **/
			    
		    for(int i = 0; i<discipline.size(); i++)
		    {
		    	disciplinesAl.add(discipline.get(i).toString());
		    }    
				    
		    /** Populate disciplines table **/
				    
		    pdb.setDisciplines(PopulateDatabase.major(disciplinesAl));
				    
		    /** Get all universities **/
			    
		    for(int i = 0; i<universities.size(); i++)
		    {
		    	Vector tuple = (Vector)universities.get(i);
				        
			ArrayList<String> cal = new ArrayList<String>();
				        
			cal.add(tuple.get(0).toString());
				        
			Vector u = (Vector)tuple.get(1);
				
			for(int j = 0; j<u.size(); j++)
			{
				cal.add(u.get(j).toString());
		    }
		       	universitiesAl.add(cal);
		    }
				    
	   		/** Populate university table **/
				    
		    pdb.setUniversities(PopulateDatabase.university(universitiesAl));
		    
		    request.setAttribute("populateDatabaseBean", pdb);
	 	}	
		
		return mapping.findForward(Constants.SUCCESS);
		
	}
}